package com.top.ruizhong.main.video

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
import com.google.android.exoplayer2.ui.PlayerView
import com.google.android.exoplayer2.util.EventLogger
import com.top.ruizhong.R
import com.top.ruizhong.entity.AudioListEntity
import com.top.ruizhong.mode.manager.PlayerManager


class PlayerEXOAdapter :
    BaseQuickAdapter<AudioListEntity, BaseViewHolder>(R.layout.item_video_player) {

    init {
        addChildClickViewIds(R.id.tvControlsDiscuss)
    }

    private val playerMaps: LinkedHashMap<Int, SimpleExoPlayer?> = linkedMapOf()

    override fun convert(holder: BaseViewHolder, item: AudioListEntity) {

        holder.setText(R.id.tvControlsName, "用户ID${item.user_id}/${item.id}")
            .setText(R.id.tvControlsTitle, item.title)
            .setText(R.id.tvControlsApprove, item.fabulous)
            .setText(R.id.tvControlsDiscuss, item.comment)
            .setText(R.id.tvControlsShare, item.forward)

        val expViewPlayer: PlayerView =
            holder.getView<PlayerView>(R.id.expViewPlayer).apply {
                controllerShowTimeoutMs = 0
                controllerHideOnTouch = false
                setShowBuffering(PlayerView.SHOW_BUFFERING_WHEN_PLAYING)
            }
        expViewPlayer.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM

        var player: SimpleExoPlayer? = findPlayerMaps(holder.layoutPosition)

        if (player == null) {
            player = SimpleExoPlayer.Builder(context)
                .build()
            val mMediaSource: MediaSource =
                PlayerManager.getDefault()
                    .buildDataSource(context, item.url)
            player.setMediaSource(mMediaSource)
            player.repeatMode = Player.REPEAT_MODE_ALL
            player.setThrowsWhenUsingWrongThread(true)
            player.addAnalyticsListener(EventLogger(null))
            player.prepare()

        }
        addPlayerMaps(holder.layoutPosition, player)

        expViewPlayer.player = player

        expViewPlayer.setOnClickListener {
            autoPlayPause(holder.layoutPosition)
        }

        if (holder.layoutPosition == 0) {
            play(holder.layoutPosition)
        }
    }

    private fun addPlayerMaps(position: Int, player: SimpleExoPlayer?) {
        playerMaps[position] = player
    }

    private fun findPlayerMaps(position: Int): SimpleExoPlayer? {
        return playerMaps[position]
    }

    private fun removePlayerMaps(position: Int) {
        playerMaps.remove(position)
    }

    private fun clearPlayerMaps() {
        playerMaps.clear()
    }

    fun autoPlayPause(position: Int) {
        findPlayerMaps(position)?.let {
            if (it.isPlaying) {
                it.pause()
            } else {
                it.play()
            }
        }
    }

    fun play(position: Int) {
        findPlayerMaps(position)?.let {
            if (!it.isPlaying) {
                it.seekTo((0))
                it.play()
            }
        }
    }

    fun pause(position: Int) {
        findPlayerMaps(position)?.let {
            if (it.isPlaying)
                it.pause()
        }
    }

    fun release(position: Int) {
        findPlayerMaps(position)?.let {
            it.clearVideoDecoderOutputBufferRenderer()
            it.stop((true))
            it.release()
            removePlayerMaps(position)
        }
    }

    fun releaseAll() {
        for ((_: Int, player: SimpleExoPlayer?) in playerMaps) {
            player?.clearVideoDecoderOutputBufferRenderer()
            player?.stop((true))
            player?.release()
        }
        clearPlayerMaps()
    }

    fun onResume(position: Int) {
        playerView(position)?.onResume()
    }

    fun onPause(position: Int) {
        playerView(position)?.onPause()
    }

    fun playerView(position: Int): PlayerView? =
        recyclerView.findViewHolderForLayoutPosition(position)
            ?.itemView?.findViewById(R.id.expViewPlayer)
}